﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.QuickStart.TelnetServer_AppServer;

namespace server
{
    class combat
    {
        Dictionary<int,combat_room> rooms;
        object lockflag = new object();

        public combat()
        {
            rooms = new Dictionary<int,combat_room>();
            for (int i = 0; i < 1000; i++)
            {
                combat_room temp = new combat_room();

                temp.room_id = i;
                rooms.Add(i, temp);
            }
        }

        public bool join(TelnetSession a)
        {
            lock (lockflag)
            {
                foreach (combat_room room in rooms.Values)
                {
                    if (room.isStart == true)
                    {
                        continue;
                    }
                    else
                    {
                        if (room.a_flag == false)
                        {
                            room.A = a;
                            room.a_flag = true;
                            room.A.Combat_id = room.room_id;
                            room.A.Combat_side = 0;
                            room.mcombat_status = combat_status.READYA;
                        }
                        else if (room.b_flag == false)
                        {
                            room.B = a;
                            room.b_flag = true;
                            room.isStart = true;
                            room.B.Combat_id = room.room_id;
                            room.B.Combat_side = 1;
                            room.mcombat_status = combat_status.READYB;
                        }
                        return true;
                    }
                }
            }
            return false;
        }

        public bool quit(TelnetSession a)
        {
            lock (lockflag)
            {
                if (a.Combat_side == 0)
                {
                    rooms[a.Combat_id].A.Combat_side = -1;
                    rooms[a.Combat_id].A.Combat_id = -1;
                    rooms[a.Combat_id].A = null;
                    rooms[a.Combat_id].a_flag = false;
                    rooms[a.Combat_id].isStart = false;
                    //todo 发送结束
                }
                else if (a.Combat_side == 1)
                {
                    rooms[a.Combat_id].B.Combat_side = -1;
                    rooms[a.Combat_id].B.Combat_id = -1;
                    rooms[a.Combat_id].B = null;
                    rooms[a.Combat_id].b_flag = false;
                    rooms[a.Combat_id].isStart = false;
                    //todo 发送结束
                }
            }
            return true;
        }

        public void do_combat()
        {
            lock (lockflag)
            {
                foreach (combat_room room in rooms.Values)
                {
                    if (room.isStart == true)
                    {
                        switch (room.mcombat_status)
                        {
                            case combat_status.READYB:
                                //下载敌人信息
                                room.A.sendCmd(room.B.Getcombat_userinfo());
                                room.B.sendCmd(room.A.Getcombat_userinfo());

                                room.A.Sendcombat_userinfo();
                                room.B.Sendcombat_userinfo();


                                room.mcombat_status = combat_status.START;
                                break;

                            case combat_status.START:
                                //下载开始信息
                                room.A.BattleStart();
                                room.B.BattleStart();

                                room.mcombat_status = combat_status.COMBATING;
                                break;

                            case combat_status.COMBATING:
                                room.A.Battle(room.B);
                                room.B.Battle(room.A);

                                room.A.AfterBattle(room.B);
                                room.B.AfterBattle(room.A);


                                room.A.sendCmd(room.B.Getcombat_userinfo());
                                room.B.sendCmd(room.A.Getcombat_userinfo());

                                room.A.BattleInfo(room.B);
                                room.B.BattleInfo(room.A);

                                if (room.A.Mcombat_userinfo.Hp <= 0 || room.B.Mcombat_userinfo.Hp <= 0)
                                {
                                    room.mcombat_status = combat_status.END;
                                }

                                break;

                            case combat_status.END:
                                if (room.B.Mcombat_userinfo.Hp <= 0)
                                {
                                    room.A.BattleVictory();
                                    room.B.BattleFailed();

                                }
                                else
                                {
                                    room.B.BattleVictory();
                                    room.A.BattleFailed();
                                }
                                room.mcombat_status = combat_status.WAITING;
                                break;
                            case combat_status.WAITING:
                                break;
                        }
                    }
                }
            }
        }
    }
    class combat_action
    {
        public Maticsoft.Model.Skill skill;
        public String attack;
        public String defence;
        public DateTime time;
    }
    class combat_room
    {
        public TelnetSession A;
        public bool a_flag;
        public TelnetSession B;
        public bool b_flag;
        public bool isStart;
        public int room_id;
        public combat_room()
        {
            a_flag = false;
            b_flag = false;
            A = null;
            B = null;
            isStart = false;
            room_id = -1;
        }
        public combat_status mcombat_status;
    }
    public enum combat_status
    {
        WAITING,
        READYA,
        READYB,
        START,
        COMBATING,
        END,
    }
    public class combat_userinfo
    {
        private string _sex;

        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }
        private int _hp;

        public int Hp
        {
            get { return _hp; }
            set { _hp = value; }
        }
        private int _mp;

        public int Mp
        {
            get { return _mp; }
            set { _mp = value; }
        }
        private int _st;

        public int St
        {
            get { return _st; }
            set { _st = value; }
        }
        private int _de;

        public int De
        {
            get { return _de; }
            set { _de = value; }
        }
        private string _baseskill;

        public string Baseskill
        {
            get { return _baseskill; }
            set { _baseskill = value; }
        }
        private string _skills;

        public string Skills
        {
            get { return _skills; }
            set { _skills = value; }
        }
        private int _totalvictory;

        public int Totalvictory
        {
            get { return _totalvictory; }
            set { _totalvictory = value; }
        }
        private int _totalfailed;

        public int Totalfailed
        {
            get { return _totalfailed; }
            set { _totalfailed = value; }
        }
        private string _nickname;

        public string Nickname
        {
            get { return _nickname; }
            set { _nickname = value; }
        }

        private int _damage_hot;

        public int Damage_hot
        {
            get { return _damage_hot; }
            set { _damage_hot = value; }
        }

        public int Head_number;
        public int Chest_number;
        public int Lag_number;

    }
}
